require("lsqlite3")

module("db", package.seeall)

function exec_statements(db, ...)
	for i = 1, select("#", ...) do
		local statement = select(i, ...)
		local result = db:exec(statement)
		if result ~= sqlite3_OK then
			print("SQL Statement failed:")
			print(statement)
			print(db:errmsg())
		end
	end
end

function create(dbname, sqlfile)
	pcall(os.remove, dbname)
	local db = sqlite3.open(dbname)
	if sqlfile then
		local f = io.open(sqlfile, "r")
		local data = f:read("*a")
		f:close()
		exec_statements(db, strsplit(";", data))
	end
	return db
end

